<template>
  <div id="vehicle-group-mapping">
    <nav aria-label="breadcrumb" role="navigation">
      <ol class="breadcrumb pb-0">
        <li class="breadcrumb-item">
          <router-link to="/admin/vehicleGroups">车型组信息</router-link>
        </li>
        <li class="breadcrumb-item active" aria-current="page">映射</li>
      </ol>
    </nav>
    <div class="card">
      <div class="card-body">
        <form class="row">
          <div class="col-1">
            <input
              type="text"
              class="form-control"
              size="8"
              placeholder="名称"
              v-model.trim="name0"
            />
          </div>
          <div class="col">
            <button
              type="button"
              class="btn btn-primary"
              @click.stop="search()"
            >
              查找
            </button>
            <button
              type="button"
              class="btn btn-secondary"
              @click.stop="reset0()"
            >
              重置
            </button>
          </div>
        </form>
      </div>
    </div>
    <table class="table table-striped table-hover table-sm">
      <thead>
        <tr>
          <th>ID</th>
          <th>名称</th>
          <th>备注</th>
          <th class="text-center">ID映射</th>
          <th></th>
        </tr>
      </thead>
      <tbody>
        <tr
          v-for="(info, index) in dataList"
          :key="info.id"
        >
          <td>{{ info.id }}</td>
          <td>{{ info.name }}</td>
          <td>{{ info.remark }}</td>
          <template v-if="info.mappings.length > 0">
            <td>
              <table class="table my-0">
                <tr v-for="m in info.mappings">
                  <td class="my-0 py-0">供应商: {{ m.supplierName }}</td>
                  <td class="my-0 py-0">
                    车型组ID: {{ m.supplierVehicleGroupId }}
                  </td>
                </tr>
              </table>
            </td>
          </template>
          <template v-else>
            <td></td>
          </template>
          <td>
            <a href="javascript:void(0)" @click="newMapping(info.id, info.name)"
              >新建映射</a
            >
          </td>
        </tr>
      </tbody>
    </table>
    <nav id="pagination-box" class="float-end">
      <my-pagination
        name="pagination"
        :row-count="sc.rowCount"
        :page-total="sc.pageTotal"
        :page-no="sc.pageNo"
        @next-page="nextPage"
        @prev-page="prevPage"
        @direct-page="directPage"
      ></my-pagination>
    </nav>

    <!-- New  Modal -->
    <div
      class="modal"
      id="modalNew"
      tabindex="-1"
      role="dialog"
      aria-labelledby="myModalLabel"
      aria-hidden="true"
    >
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <h4 class="modal-title" id="myModalLabel">新建车型组关联</h4>
            <button type="button" class="close" data-dismiss="modal">
              <span aria-hidden="true">&times;</span
              ><span class="sr-only">Close</span>
            </button>
          </div>
          <div class="modal-body">
            <form id="frmPolicy" role="form" class="form-horizontal">
              <div class="row">
                <label class="control-label col-md-4 text-end"> 名称 </label>
                <div class="col-md-6">
                  {{ vehicleName }}
                </div>
              </div>
              <div class="row">
                <label class="control-label col-md-4 text-end"> 供应商 </label>
                <div class="col-md-6">
                  <select class="form-select" v-model.number="supplierId">
                    <template v-for="info in supplierList">
                      <option :key="info.id" :value="info.supplier.id">
                        {{ getTypeDesc(info.productType) }} -
                        {{ info.supplier.name }}
                      </option>
                    </template>
                  </select>
                </div>
              </div>
              <div class="row">
                <label class="control-label col-md-4 text-end">
                  供应商车型组ID
                </label>
                <div class="col-md-6">
                  <input
                    type="text"
                    class="form-control"
                    v-model.trim="supplierVehicleId"
                  />
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">
              取消
            </button>
            <button
              type="button"
              class="btn btn-primary"
              v-on:click="saveMapping()"
            >
              保存
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations } from "vuex";
import MyPagination from "@/components/my-pagination.vue";
import $ from "jquery";
import {
  searchVehicleGroupInfos,
  searchApiProdcutSuppliers,
  saveVehicleGroupMapping,
} from "@/api/basic-data.js";

export default {
  components: {
    MyPagination,
  },
  data() {
    return {
      sc: {
        rowCount: 0,
        pageNo: 1,
        pageSize: 15,
        pageTotal: 0,
      },
      name0: "",

      id: 0,
      name: "",

      dataList: [],
      supplierList: [],

      vehicleId: 0,
      vehicleName: "",
      supplierId: 0,
      supplierVehicleId: 0,
    };
  },
  computed: {
    isAdmin() {
      return this.$store.getters.isAdmin;
    },
  },
  mounted: function () {
    this.search();
    this.searchSuppliers();
  },
  methods: {
    ...mapMutations(["showLoading", "hideLoading"]),
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    search: function () {
      const params = {
        "sc.pageNo": this.sc.pageNo,
        "sc.pageSize": this.sc.pageSize,
        "sc.name": this.name0,
        mapping: 1,
      };

      this.showLoading();

      searchVehicleGroupInfos(
        params,
        (v) => {
          this.dataList = v.dataList;
          this.sc = v.page;
        },
        () => {
          this.hideLoading();
        }
      );
    },
    searchSuppliers: function () {
      const params = {
        "sc.pageSize": 100,
      };

      searchApiProdcutSuppliers((v) => {
        this.supplierList = v;
      });
    },
    reset0: function () {
      this.supplierType0 = -1;
      this.name0 = "";
      this.scStatus = -1;
    },
    reset: function () {
      this.cityName = "";
      this.cityId = 0;
    },
    newMapping: function (vId, vName) {
      this.reset();

      this.vehicleId = vId;
      this.vehicleName = vName;

      $("#modalNew").modal();
    },
    saveMapping: function () {
      const params = {
        vehicleId: this.vehicleId,
        supplierId: this.supplierId,
        supplierVehicleId: this.supplierVehicleId,
      };
      saveVehicleGroupMapping(params, (jsonResult) => {
        if (jsonResult.status === "OK") {
          $("#modalNew").modal("hide");
          this.search();
        } else {
          this.showErrMsg("失败：" + jsonResult.errmsg, "danger");
        }
      });
    },
    prevPage: function () {
      this.sc.pageNo = this.sc.pageNo - 1;
      if (this.sc.pageNo < 1) this.sc.pageNo = 1;
      this.search();
    },
    nextPage: function () {
      this.sc.pageNo = this.sc.pageNo + 1;
      this.search();
    },
    directPage: function (pageNo) {
      this.sc.pageNo = pageNo;
      this.search();
    },
    getTypeDesc: function (val) {
      switch (val) {
        case 1:
          return "国内机票";
        case 2:
          return "国际机票";
        case 4:
          return "国内酒店";
        case 8:
          return "国际酒店";
        case 32:
          return "国内租车";
        case 64:
          return "国际租车";
        case 128:
          return "机场服务";
      }
    },
  },
};
</script>
